import { Config } from "@/config/Config";
import { HttpClient } from "@/httpclient/HttpClient";
import { DateUtil } from "@/utils/DateUtil";
import { Logger } from "@/utils/Logger";
import { RandomUtil } from "@/utils/RandomUtil";
import { UIHelper } from "@/utils/UIHelper";
import { DeviceModel } from "./DeviceModel";
import { GameModel } from "./GameModel";
import { ZoneModel } from "./ZoneModel";

export class DailyDataSummaryModel {
    //每日数据汇总

    public time: number = 0; //日期
    public gameId: number = 0; //游戏
    public channelId: number = 0;
    public deviceType: number = DeviceModel.DeviceType.All; //设备类型
    public zoneId: number = 0; //区服Id
    public registerAccountNum: number = 0; //注册账号数
    public registerRoleNum: number = 0; //注册角色数
    public totalLoginNum: number = 0;//总登录数
    public averageOnline: number = 0; //平均在线
    public averageNeiGuaOnline: number = 0; //平均内挂在线
    public chargePeopleNum: number = 0; //充值人数
    public chargeTimes: number = 0; //充值次数
    public chargeMoney: number = 0; //充值金额
    public arpu: string = "0"; //当日所选区服的充值金额/当日所选区服的充值人数
    public chargePercent: string = "";   //充值人数/登录数*100%    //付费率
    public newRegisterCharge: number = 0; //新增充值人数  当日所选区服新注册【账号】的充值人数
    public newChargePercent: string = ""; //新增付费率 新增充值人数/注册数
    public newChargeMoney: number = 0;  //新增充值金额 当日所选区服新注册【账号】的充值金额
    public newArpu: string = "";// 新增充值金额/新增充值人数
    public oldCharge: number = 0; //非当天注册且当天有登录过的玩家总数所充值的金额
    public accountLTV: string = ""; //新增充值金额/注册数
    public roleLTV: string = ""; //新增充值金额/角色数

    public static modelList: Array<DailyDataSummaryModel> = [];

    public static totalNum: number = 50;

    public static initTest() {
        this.modelList = [];
        for (let i = 0; i < this.totalNum; i++) {
            let model: DailyDataSummaryModel = new DailyDataSummaryModel();
            model.channelId = RandomUtil.nextInt(1, 4)
            model.time = DateUtil.now() + RandomUtil.nextInt(-99999, 999999);
            model.gameId = GameModel.instance.gameId;
            model.deviceType = DeviceModel.DeviceType.Android;
            model.zoneId = RandomUtil.nextInt(1, 5);
            model.registerAccountNum = RandomUtil.nextInt(1, 99999);
            model.registerRoleNum = RandomUtil.nextInt(1, 9999);
            model.totalLoginNum = RandomUtil.nextInt(1, 9999);
            model.averageOnline = RandomUtil.nextInt(1, 999);
            model.averageNeiGuaOnline = RandomUtil.nextInt(1, 99899);
            model.chargePeopleNum = RandomUtil.nextInt(1, 9989)
            model.chargeTimes = RandomUtil.nextInt(1, 54555);
            model.chargeMoney = RandomUtil.nextInt(1, 54555);
            model.arpu = (model.chargeMoney / model.chargePeopleNum).toFixed(2) + "";
            model.chargePercent = (model.chargePeopleNum / model.totalLoginNum*100).toFixed(2) + "%"
            model.newRegisterCharge = RandomUtil.nextInt(1, 99999)
            model.newChargeMoney = RandomUtil.nextInt(1, 99999);
            model.newChargePercent = (model.newRegisterCharge / model.registerAccountNum*100).toFixed(2) + "%";
            model.newArpu = (model.newChargeMoney / model.newRegisterCharge).toFixed(2) + "";
            model.oldCharge = RandomUtil.nextInt(1, 99999)
            model.accountLTV = (model.newChargeMoney / model.registerAccountNum).toFixed(2) + ""
            model.roleLTV = (model.newChargeMoney / model.registerRoleNum ).toFixed(2) + ""
            this.modelList[i] = model;
        }
        // Logger.log("DailyDataSummaryModel.initTest", this.modelList)
    }

    public static json2ModelList(jsonData: object) {
        let data: Array<object> = jsonData["data"]
        this.modelList = [];
        for (let i = 0; i < data.length; i++) {
            let model: DailyDataSummaryModel = new DailyDataSummaryModel();
            model.time = data[i]["time"] * 1000
            model.registerAccountNum = data[i]["register_num"]
            model.registerRoleNum = data[i]["create_role_num"];
            model.totalLoginNum = data[i]["login_num"]
            model.averageOnline = data[i]["avg_online_num"]
            model.averageNeiGuaOnline = data[i]["avg_auto_online_num"]
            model.chargePeopleNum = data[i]["pay_role_num"]
            model.chargeTimes = data[i]["pay_times"]
            model.chargeMoney = data[i]["pay_num"]
            model.arpu = (model.chargeMoney / model.chargePeopleNum).toFixed(2) + "";
            model.chargePercent = (model.chargePeopleNum / model.totalLoginNum*100).toFixed(2) + ""
            model.newRegisterCharge = data[i]["new_pay_role_num"]
            model.newChargePercent = (model.newRegisterCharge / model.registerAccountNum *100).toFixed(2) + "%";
            model.newChargeMoney = data[i]["new_pay_num"]
            model.newArpu = (model.newChargeMoney / model.newRegisterCharge).toFixed(2) + "";
            model.oldCharge = data[i]["old_pay_num"]
            model.accountLTV = (model.newChargeMoney / model.registerAccountNum ).toFixed(2) + ""
            model.roleLTV = (model.newChargeMoney / model.registerRoleNum ).toFixed(2) + ""

            if(model.chargePeopleNum == 0){
                model.arpu = "0"
            }
            if(model.totalLoginNum == 0){
                model.chargePercent = "0%"
            }
            if(model.registerAccountNum == 0){
                model.newChargePercent = "0%"
                model.accountLTV = "0"
            }
            if(model.newRegisterCharge == 0){
                model.newArpu ="0"
            }
            if(model.registerRoleNum == 0){
                model.roleLTV ="0"
            }

            this.modelList[i] = model;
        }
    }

    public static getShowList(channelId: number, listQuery: any, server_idList: Array<number>, device: number, start_time: number, end_time: number, callback: Function) {
        let showList: Array<DailyDataSummaryModel> = [];
        start_time = Math.floor(start_time / 1000);
        end_time = Math.floor(end_time / 1000);//变为秒
        let tempServer_id:Array<number> = server_idList.concat();
        Logger.log("DailyDataSummaryModel.getShowList",tempServer_id.length,  ZoneModel.selectList, channelId, server_idList, device, start_time, end_time);
        if (Config.isConnectServer) {
            if (tempServer_id.length == 0) {
                for (let i = 0; i < ZoneModel.selectList.length; i++) {
                    let model:ZoneModel = ZoneModel.selectList[i];
                    tempServer_id.push(model.serverId);
                }
            }
            if (tempServer_id.length > 0) {
                HttpClient.query_data_view(channelId, tempServer_id, device, start_time, end_time, listQuery["page"], listQuery["limit"], () => {
                    showList = this.modelList
                    callback && callback(showList);
                });
            } else {
                UIHelper.showMsg("服务器列表为空")
                callback && callback(showList);
            }
        } else {
            let startIndex: number = (listQuery["page"] - 1) * listQuery["limit"]
            let endEndex: number = startIndex + listQuery["limit"]
            for (let i = startIndex; i < endEndex; i++) {
                let model: DailyDataSummaryModel = this.modelList[i];
                if (model) {
                    showList.push(model)
                }
            }
            Logger.log("DailyDataSummaryModel.getShowList", this.modelList)
            DailyDataSummaryModel.totalNum = this.modelList.length
            callback && callback(showList);
        }
        return showList
    }



}